package com.example.back_code.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.back_code.entity.po.HorizontalProject;
import com.example.back_code.mapper.HorizontalProjectMapper;
import com.example.back_code.service.HorizontalProjectService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class HorizontalProjectServiceImpl extends ServiceImpl<HorizontalProjectMapper, HorizontalProject> implements HorizontalProjectService {
    @Override
    public boolean auditProject(Long id, Integer status, String rejectReason) {
        HorizontalProject project = getById(id);
        if (project == null) return false;
        project.setStatus(status);
        if (status != null && status == 2) {
            project.setRejectReason(rejectReason);
        }
        return updateById(project);
    }

    @Override
    public List<HorizontalProject> filterProjects(String projectName, String startDate, String endDate, Integer status) {
        QueryWrapper<HorizontalProject> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(projectName)) {
            wrapper.like("project_name", projectName);
        }
        if (StringUtils.hasText(startDate)) {
            wrapper.ge("report_date", startDate);
        }
        if (StringUtils.hasText(endDate)) {
            wrapper.le("report_date", endDate);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        return list(wrapper);
    }
}
